﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class HRM_GROUP
    {
        private string m_DepartmentCode;
        private string m_DepartmentName;
        private string m_Description;
        private int m_FactQuantity;
        private string m_GroupCode;
        private string m_GroupName;
        private int m_Quantity;

        public HRM_GROUP()
        {
            m_GroupCode = "";
            m_DepartmentCode = "";
            m_GroupName = "";
            m_Quantity = 0;
            m_FactQuantity = 0;
            m_Description = "";
        }

        public HRM_GROUP(string GroupCode, string DepartmentCode, string GroupName, int Quantity, int FactQuantity,
            string Description)
        {
            m_GroupCode = GroupCode;
            m_DepartmentCode = DepartmentCode;
            m_GroupName = GroupName;
            m_Quantity = Quantity;
            m_FactQuantity = FactQuantity;
            m_Description = Description;
        }

        [DisplayName("DepartmentCode"), Category("Primary Key")]
        public string DepartmentCode
        {
            get { return m_DepartmentCode; }
            set { m_DepartmentCode = value; }
        }

        [Category("Column"), DisplayName("Description")]
        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        [DisplayName("FactQuantity"), Category("Column")]
        public int FactQuantity
        {
            get { return m_FactQuantity; }
            set { m_FactQuantity = value; }
        }

        [Category("Primary Key"), DisplayName("GroupCode")]
        public string GroupCode
        {
            get { return m_GroupCode; }
            set { m_GroupCode = value; }
        }

        [DisplayName("GroupName"), Category("Column")]
        public string GroupName
        {
            get { return m_GroupName; }
            set { m_GroupName = value; }
        }

        public string ProductName
        {
            get { return "Class HRM_GROUP"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        [Category("Column"), DisplayName("Quantity")]
        public int Quantity
        {
            get { return m_Quantity; }
            set { m_Quantity = value; }
        }

        public void AddAllCheckedListBoxEdit(CheckedListBoxControl imgCombo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                var item = new CheckedListBoxItem
                {
                    Description = row["GroupName"] + " (" + row["GroupCode"] + ")",
                    CheckState = CheckState.Checked,
                    Value = row["GroupCode"].ToString()
                };
                imgCombo.Items.Add(item);
            }
        }

        public void AddCheckedListBoxEdit(CheckedListBoxControl imgCombo, string DepartmentCode)
        {
            var listByDepartment = new DataTable();
            listByDepartment = GetListByDepartment(DepartmentCode);
            foreach (DataRow row in listByDepartment.Rows)
            {
                var item = new CheckedListBoxItem
                {
                    Description = row["GroupName"] + " (" + row["GroupCode"] + ")",
                    CheckState = CheckState.Checked,
                    Value = row["GroupCode"].ToString()
                };
                imgCombo.Items.Add(item);
            }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "GroupName", "GroupCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["GroupCode"] = "All";
            row["GroupName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "GroupName", "GroupCode");
        }

        public void AddGridLookupEdit(GridLookUpEdit gridlookup, string DepartmentCode)
        {
            var listByDepartment = new DataTable();
            listByDepartment = GetListByDepartment(DepartmentCode);
            gridlookup.Properties.DataSource = listByDepartment;
            gridlookup.Properties.DisplayMember = "GroupName";
            gridlookup.Properties.ValueMember = "GroupCode";
        }

        public void AddGridLookupEdit(GridLookUpEdit gridlookup, string DepartmentCode, string ValueMember)
        {
            var listByDepartment = new DataTable();
            listByDepartment = GetListByDepartment(DepartmentCode);
            gridlookup.Properties.DataSource = listByDepartment;
            gridlookup.Properties.DisplayMember = "GroupName";
            gridlookup.Properties.ValueMember = ValueMember;
        }

        public string Delete()
        {
            string[] myParams = {"@GroupCode"};
            object[] myValues = {m_GroupCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_GROUP_Delete", myParams, myValues);
        }

        public string Delete(string GroupCode)
        {
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_GROUP_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string GroupCode)
        {
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_GROUP_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string GroupCode)
        {
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_GROUP_Delete", myParams, myValues);
        }

        public bool Exist(string GroupCode)
        {
            bool hasRows = false;
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("HRM_GROUP_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string GroupCode)
        {
            string str = "";
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("HRM_GROUP_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_GroupCode = Convert.ToString(reader["GroupCode"]);
                    m_DepartmentCode = Convert.ToString(reader["DepartmentCode"]);
                    m_GroupName = Convert.ToString(reader["GroupName"]);
                    m_Quantity = Convert.ToInt32(reader["Quantity"]);
                    m_FactQuantity = Convert.ToInt32(reader["FactQuantity"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string PositionCode)
        {
            string str = "";
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "HRM_GROUP_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_GroupCode = Convert.ToString(reader["GroupCode"]);
                    m_DepartmentCode = Convert.ToString(reader["DepartmentCode"]);
                    m_GroupName = Convert.ToString(reader["GroupName"]);
                    m_Quantity = Convert.ToInt32(reader["Quantity"]);
                    m_FactQuantity = Convert.ToInt32(reader["FactQuantity"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string GroupCode)
        {
            string str = "";
            string[] myParams = {"@GroupCode"};
            object[] myValues = {GroupCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "HRM_GROUP_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_GroupCode = Convert.ToString(reader["GroupCode"]);
                    m_DepartmentCode = Convert.ToString(reader["DepartmentCode"]);
                    m_GroupName = Convert.ToString(reader["GroupName"]);
                    m_Quantity = Convert.ToInt32(reader["Quantity"]);
                    m_FactQuantity = Convert.ToInt32(reader["FactQuantity"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("HRM_GROUP_GetList");
        }

        public DataTable GetListByDepartment(string DepartmentCode)
        {
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("HRM_GROUP_GetListByDepartment", myParams, myValues);
        }

        public string Insert()
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {m_GroupCode, m_DepartmentCode, m_GroupName, m_Quantity, m_FactQuantity, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_GROUP_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {m_GroupCode, m_DepartmentCode, m_GroupName, m_Quantity, m_FactQuantity, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_GROUP_Insert", myParams, myValues);
        }

        public string Insert(string GroupCode, string GroupName, int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {GroupCode, DepartmentCode, GroupName, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_GROUP_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string GroupCode, string DepartmentCode, string GroupName,
            int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {GroupCode, DepartmentCode, GroupName, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_GROUP_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string GroupCode, string DepartmentCode, string GroupName,
            int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {GroupCode, DepartmentCode, GroupName, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_GROUP_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("HRM_GROUP", "GroupCode", "TN");
        }

        public string Update()
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {m_GroupCode, m_DepartmentCode, m_GroupName, m_Quantity, m_FactQuantity, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_GROUP_Update", myParams, myValues);
        }

        public string Update(string GroupCode, string DepartmentCode, string GroupName, int Quantity, int FactQuantity,
            string Description)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {GroupCode, DepartmentCode, GroupName, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_GROUP_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string GroupCode, string DepartmentCode, string GroupName,
            int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {GroupCode, DepartmentCode, GroupName, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_GROUP_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string GroupCode, string DepartmentCode, string GroupName,
            int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@GroupCode", "@DepartmentCode", "@GroupName", "@Quantity", "@FactQuantity",
                "@Description"
            };
            object[] myValues = {GroupCode, DepartmentCode, GroupName, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_GROUP_Update", myParams, myValues);
        }
    }
}